"""
宏观经济数据采集定时任务
"""

from celery import shared_task
import logging
from modules.tasks.celery_app import app
from modules.models.base import get_db
from modules.models.stock import SecurityInfo
from modules.data_collector.news_fundamental_collector import NewsFundamentalCollector
from modules.data_collector.macro_collector import MacroCollector

logger = logging.getLogger(__name__)

@shared_task(bind=True, name="collect_macro_economic_data")
def collect_macro_economic_data(self):
    """定时采集宏观经济指标"""
    try:
        collector = NewsFundamentalCollector()
        
        # 采集GDP数据
        gdp_count = collector.collect_macro_data("gdp", "中国")
        
        # 采集CPI数据
        cpi_count = collector.collect_macro_data("cpi", "中国")
        
        # 采集PPI数据
        ppi_count = collector.collect_macro_data("ppi", "中国")
        
        # 采集失业率数据
        unemployment_count = collector.collect_macro_data("unemployment", "中国")
        
        results = {
            "gdp": gdp_count,
            "cpi": cpi_count,
            "ppi": ppi_count,
            "unemployment": unemployment_count
        }
        
        return {
            "status": "success",
            "results": results
        }
    except Exception as e:
        logger.error(f"宏观经济数据采集失败: {str(e)}")
        return {
            "status": "error",
            "error": str(e)
        }